<?php
require_once('../../config.php');
function sql_filters($field, $value){
	$rf = array();
	//$req_filters = explode(',', $value);
	foreach($value as $req_filter){
		$rf[] = $field.'='.$req_filter;
	}
	$rf_sql = implode(' OR ', $rf);
	return '('.$rf_sql.')';
}

function libres_comments($comments, $id, $action){	
	global $CFG;
	$record = get_record('libres', 'id', $id);
	if($action == 'form'){
		$output = '<div style="text-align:center;">Notes/Comments:<br /><div id="existing_notes'.$id.'">'.$record->notes.'</div>Add notes:<br /><form id="commentform'.$id.'" name="commentform'.$id.'"><input type="text" id="comments'.$id.'" name="comments'.$id.'" value="" />';
		$output .= '</form>';
		$output .= '<p style="text-align:center;"><a href="#" onclick="comment=document.commentform'.$id.'.comments'.$id.'.value;save_comment(8, \''.$CFG->wwwroot.'\', comment, '.$id.');return false;">Save</a></div><p>';
		return $output;
	}
	$record->notes .= '<p>'.$comments.'</p>';
	$success = update_record('libres', $record);
	if($success = update_record('libres', $record)){
		return $record->notes;
	}else{
		return 'Sorry, an error occurred.';
	}
}

function libres_pagination($count, $rec_per_pg, $page, $arg_str, $curr_page){
	global $CFG;
	$output = '<div>Pages: ';
	$last = end(range(1, ceil(($count+1)/$rec_per_pg)));
	foreach(range(1, ceil(($count+1)/$rec_per_pg)) as $res_number){
		$link_offset = ($res_number -1)*$rec_per_pg;
		if($res_number == $curr_page){
			$num_style = 'style="background-color:#ccc;" ';
		}else{
			$num_style= '';
		}
		$output .= '<a '.$num_style.'href="'.$CFG->wwwroot.'/blocks/moodlelibreserve/'.$page.'?offset='.$link_offset.'&'.$arg_str.'">'.$res_number.'</a> ';
	}
	$output .= '</div>';
	return $output;
}

class filter_cookie {
	function set_cookie($cookie_name, $cookie_value){
		$val = '';
		foreach(array_keys($cookie_value) as $key){
			$val .= '&'.$key.'='.$cookie_value[$key];
		}
		return setcookie($cookie_name, $val, time()+604800);
	}

	function read_cookie($cookie_name){
		if(isset($_COOKIE[$cookie_name])){
			return $_COOKIE[$cookie_name];
		}
		return;
	}
}
?>
